package pages;

import org.openqa.selenium.By;
import selenium.WebDriverWrapper;
import utils.Log4Test;

/**
 * Created by sergey on 02.08.2014.
 */
public class PickYourDiamondsPage extends BasicPage {
    private  static  final String UNIVERSAL_SHAPE_LOCATOR = "//ol[@class='m-filter-item-list ']/li/a[contains(text(),'%s')]";
    private  static  final By SELECTED_SHAPE_LOCATOR = By.xpath("//span[@class='m-selected-filter-item']");
    private  static  final By SELECTED_SHAPE_AMOUNT_LOCATOR = By.xpath("//ol[@class='m-filter-item-list ']/li[@class='m-selected-ln-item']");
    private static final By AMOUNT_LOCATOR = By.xpath("//p[@class='amount']/strong");
    private static final By FIRST_ITEM_OF_PRODUCTS_GRID_LOCATOR = By.xpath("//li[@class='item first']");
    private static final By ITEM_OF_PRODUCTS_GRID_LOCATOR = By.xpath("//li[@class='item']");
    private static final By LAST_ITEM_OF_PRODUCTS_GRID_LOCATOR = By.xpath("//li[@class='item last']");
    private static final By ADD_TO_CART_FIRST_PRODUCT_LOCATOR = By.xpath("//a[@href='http://www.freddydiamonds.com/cheap-diamonds/0-5-carat-cushion-cut-diamond.html']/ancestor::li/div[@class='actions']/button");

    public PickYourDiamondsPage(WebDriverWrapper driver)
    {
        super (driver);
        URL = "http://www.freddydiamonds.com/cheap-diamonds.html";
        driver.get(URL);
    }
    public String findStringContainingAmount(String shape)
    {
        driver.findElement(By.xpath(String.format(UNIVERSAL_SHAPE_LOCATOR, shape))).click();
        String shapeAndAmountString = driver.findElement(SELECTED_SHAPE_AMOUNT_LOCATOR).getText();
        Log4Test.info("The expected number of elements of this shape = " + shapeAndAmountString);
        return shapeAndAmountString;
    }
    public boolean amountOfProductsOfSameShapeChecker(int amount)
    {
        Integer countedAmount = driver.findElements(FIRST_ITEM_OF_PRODUCTS_GRID_LOCATOR).size()+driver.findElements(ITEM_OF_PRODUCTS_GRID_LOCATOR).size()+driver.findElements(LAST_ITEM_OF_PRODUCTS_GRID_LOCATOR).size();
        Log4Test.info("Amount of diamonds of selected shape = " + countedAmount);
        return countedAmount.equals(amount);
    }
    public String findAmount()
    {
        return driver.findElement(AMOUNT_LOCATOR).getText().substring(0,1);
    }
    public void addDiamondToCart()
    {
        driver.findElement(ADD_TO_CART_FIRST_PRODUCT_LOCATOR).click();
        Log4Test.info("Diamond has been added to the cart");
    }

}